#define  _CRT_SECURE_NO_WARNINGS 1
#include"Seqlist.h"

//初始化
void SLInit(SL* ps) {
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLPushBack(SL* ps, SLDataType x)
{
	//温柔的处理方式
	if (ps == NULL)
	{
		return 1;
	}
	//粗暴--断言
	assert(ps != NULL);
	//空间不足，申请空间
	void SLCheckCapacity(SL* ps)；
	ps->arr[ps->size++] = x;

}

void SLCheckCapacity(SL* ps)//检查空间
{
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		//空间不够
		//增加空间
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			return 1;
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}
void SLPushFront(SL * ps, SLDataType x)
{
	assert(ps);
    SLCheckCapacity(ps);//检查空间大小
	//直接头插

	
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	++ps->size;
};
//尾删
void SLPopBack(SL* ps)
{
	assert(ps && ps->size > 0);
	--ps->size;
}
//头删
void SLPopFront(SL* ps)
{
	assert(ps && ps->size > 0);
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}
void SLPront(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ",ps->arr[i]);
	}
	printf("\n");
}
//查找x，找到了就返回对应数据下标
int SLFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			//找到了返回下标
			return i;
		}
		return -1;//返回无效下标
	}
}


void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i-1];
	}
	ps->arr[pos] = x;
	++ps->size;
}